load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
load("//bazel:canisters.bzl", "rust_canister")
load("//bazel:prost.bzl", "generated_files_check")

package(default_visibility = ["//visibility:public"])

filegroup(
    name = "protos",
    srcs = glob(["proto/**/*.proto"]),
)

DEPENDENCIES = [
    # Keep sorted.
    "//packages/ic-http-types",
    "//packages/icrc-ledger-types:icrc_ledger_types",
    "//rs/nervous_system/clients",
    "//rs/nervous_system/common",
    "//rs/nervous_system/proto",
    "//rs/nervous_system/root",
    "//rs/nervous_system/runtime",
    "//rs/rust_canisters/canister_log",
    "//rs/sns/swap",  # TODO[NNS1-2282]
    "//rs/types/base_types",
    "//rs/types/management_canister_types",
    "@crate_index//:build-info",
    "@crate_index//:candid",
    "@crate_index//:comparable",
    "@crate_index//:futures",
    "@crate_index//:ic-cdk",
    "@crate_index//:ic-cdk-timers",
    "@crate_index//:ic-metrics-encoder",
    "@crate_index//:prost",
    "@crate_index//:serde",
]

MACRO_DEPENDENCIES = [
    # Keep sorted.
    "//rs/nervous_system/common/build_metadata",
    "@crate_index//:async-trait",
]

BUILD_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:build-info-build",
]

ALIASES = {}

cargo_build_script(
    name = "build_script",
    srcs = ["build.rs"],
    aliases = ALIASES,
    data = [],  # build script data (e.g. template files) goes here
    version = "0.9.0",
    deps = BUILD_DEPENDENCIES,
)

rust_library(
    name = "root",
    srcs = glob([
        "src/**",
    ]),
    aliases = ALIASES,
    crate_name = "ic_sns_root",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "0.9.0",
    deps = DEPENDENCIES + [":build_script"],
)

rust_canister(
    name = "sns-root-canister",
    srcs = ["canister/canister.rs"],
    aliases = ALIASES,
    compile_data = ["canister/root.did"],
    proc_macro_deps = MACRO_DEPENDENCIES,
    service_file = ":canister/root.did",
    deps = DEPENDENCIES + [
        ":build_script",
        ":root",
    ],
)

generated_files_check(
    name = "check_generated_files",
    srcs = ["tests/check_generated_files.rs"],
    data = glob(["src/gen/*.rs"]) + [
        ":protos",
        "//rs/nervous_system/proto:protos",
        "//rs/types/base_types:protos",
    ],
    manifest_dir = "rs/sns/root",
    deps = [
        # Keep sorted.
        "//rs/sns/root/protobuf_generator:lib",
        "//rs/test_utilities/compare_dirs",
        "@crate_index//:tempfile",
    ],
)

DEV_DEPENDENCIES = [
    # Keep sorted.
    "//rs/sns/root/protobuf_generator:lib",
    "@crate_index//:candid_parser",
    "@crate_index//:maplit",
    "@crate_index//:tokio",
]

MACRO_DEV_DEPENDENCIES = []

rust_test(
    name = "root_test",
    aliases = ALIASES,
    crate = ":root",
    data = [
        "//rs/sns/root:proto/ic_sns_root/pb/v1/root.proto",
        "//rs/types/base_types:proto/ic_base_types/pb/v1/types.proto",
    ],
    env = {
        "CARGO_MANIFEST_DIR": "rs/sns/root/",
    },
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES + [":build_script"],
)

rust_test(
    name = "canister_unit_test",
    srcs = glob(["canister/**/*.rs"]),
    compile_data = ["canister/root.did"],
    crate_root = "canister/canister.rs",
    proc_macro_deps = MACRO_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES + [
        ":build_script",
        ":root",
    ],
)
